package com.lg.baselibrary.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * Created by LiGang on 2017/5/27.
 */

public abstract class BaseActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int contentViewLayout = provideContentViewLayout();
        if(contentViewLayout == 0){
            throw new RuntimeException("please provide ContentViewLayout");
        }
        setContentView(contentViewLayout);
        initView();
        initData();
    }


    protected abstract int provideContentViewLayout();
    protected abstract void initView();
    protected abstract void initData();
    protected <T extends View> T findView(int viewId){
        return (T) findViewById(viewId);
    }

    protected void startActivity(Class<?> clazz){
        startActivityForResult(clazz,-1,null);
    }
    protected void startActivity(Class<?> clazz,Bundle bundle){
        startActivityForResult(clazz,-1,bundle);
    }
    protected void startActivityForResult(Class<?> clazz,int requestCode){
        startActivityForResult(clazz,requestCode,null);
    }
    protected void startActivityForResult(Class<?> clazz,int requestCode,Bundle bundle){
        Intent intent = new Intent(this,clazz);
        startActivityForResult(intent,requestCode,bundle);
    }
    /**
     * 获取状态栏高度
     */
    public int getStatusHeight() {
        final Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int n = rect.top;
        if (n != 0) {
            return n;
        }
        try {
            final Class<?> forName = Class.forName("com.android.internal.R$dimen");
            n = getResources().getDimensionPixelSize(Integer.parseInt(forName.getField("status_bar_height").get(forName.newInstance()).toString()));
            return n;
        }catch (Exception ex) {
            return n;
        }
    }
    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     */
    protected  int px2dip(float pxValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     */
    public  int dip2px( float dipValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     */
    public  int px2sp(float pxValue) {
        final float fontScale = getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     */
    public  int sp2px(float spValue) {
        final float fontScale = getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
